#include "../Logger/Logger.h"
#include "Person.pb.h"

#include <string>

using namespace std;

void logger_init()
{
    // 配置日志系统
    LoggerConfig config;
    config.baseFileName = "./log/unique_ptr_log";
    config.maxFileSize = 1024 * 1024; // 1MB
    config.maxFiles = 3;
    config.consoleOutput = true;
    config.level = spdlog::level::debug;

    // 初始化日志
    Logger::init(config);

    LOG_INFO("我是王乾");
}

int main()
{
    system("chcp 65001");

    logger_init();

    // 验证protobuf版本
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    Person p1;
    p1.set_id(1);
    p1.set_sex("male");
    p1.set_age(26);
    p1.add_name("小明");
    p1.add_name("小红");

    Address *addre1 = p1.add_addr();
    addre1->set_addr("广州省广州市");
    addre1->set_num(1);

    Address *addre2 = p1.add_addr();
    addre2->set_addr("广西省南宁市");
    addre2->set_num(2);

    string output;
    p1.SerializeToString(&output);

    Person p2;
    p2.ParseFromString(output);

    LOG_INFO("p2 id:{} age:{} sex:{}", p2.id(), p2.age(), p2.sex());

    int name_size = p2.name_size();
    for (int i = 0; i < name_size; i++) {
        LOG_INFO("name:{}", p2.name(i));
    }

    int address_size = p2.addr_size();
    for (int i = 0; i < address_size; i++) {
        LOG_INFO("addr:{} num:{}", p2.addr(i).addr(), p2.addr(i).num());
    }

    // 清理protobuf库
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}
